package com.jaks.xpense;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jaks.xpense.data.Expenses;
import com.jaks.xpense.data.entities.ExpenseCategory;
import com.jaks.xpense.utils.Globals;

public class FilterByCategoryActivity extends ListActivity {

	private Expenses expenses;
	private ArrayList<ExpenseCategory> categories;

	private boolean isSearchMode;
	private String from;
	private String to;
	private long[] selectedTagIds;
	private long paymentType;

	private TextView totalView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		paymentType = 0;
		categories = null;
		expenses = new Expenses(this);
		setContentView(R.layout.filterexpenses);
		getListView().setDividerHeight(1);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			isSearchMode = true;
			String[] dates = extras.getStringArray("dates");
			from = dates[0];
			to = dates[1];

			selectedTagIds = extras.getLongArray(Globals.SelectedTagIds);
			paymentType = extras.getLong("paymentType");
		} else {
			isSearchMode = false;
		}
		totalView = (TextView) findViewById(R.id.searchTotal);
		totalView.setText(Globals.Empty);
	}

	@Override
	protected void onStart() {
		super.onStart();

		if (isSearchMode) {
			categories = expenses.getFilteredCategories(from, to, selectedTagIds, paymentType);
		} else {
			categories = expenses.getAllCategories();
		}

		populate();
		totalView.setText(Globals.toDecimalText(getTotal()));
	}

	private double getTotal() {
		double total = 0;

		if ((categories != null) && (categories.size() > 0)) {
			for (ExpenseCategory category : categories) {
				total += category.getAmount();
			}
		}

		return total;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		expenses.close();
	}

	private void populate() {

		ArrayAdapter<ExpenseCategory> adapter = new ExpenseCategoryArrayAdapter(this,
		    R.layout.tile_category_row, R.id.categoryText, R.id.categoryTotal, categories);

		Comparator<ExpenseCategory> comparator = new Comparator<ExpenseCategory>() {

			@Override
			public int compare(ExpenseCategory lhs, ExpenseCategory rhs) {
				return lhs.getName().compareTo(rhs.getName());
			}
		};

		adapter.sort(comparator);
		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		ExpenseCategory category = categories.get(position);
		search(category.getIds());
	}

	private void search(String ids) {
		Intent intent = new Intent(this, FilterExpensesActivity.class);
		intent.putExtra("expenseIds", ids);
		startActivityForResult(intent, 0);
	}

	private class ExpenseCategoryArrayAdapter extends ArrayAdapter<ExpenseCategory> {

		private final Activity context;
		private final List<ExpenseCategory> categories;
		private final int resource;
		private final int categoryResourceId;
		private final int totalResourceId;

		public ExpenseCategoryArrayAdapter(Activity context, int resource, int categoryResourceId,
		    int totalResourceId, List<ExpenseCategory> categories) {
			super(context, resource, categoryResourceId, categories);
			this.context = context;
			this.categories = categories;
			this.resource = resource;
			this.categoryResourceId = categoryResourceId;
			this.totalResourceId = totalResourceId;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			LayoutInflater inflater = context.getLayoutInflater();
			View view = inflater.inflate(resource, null, true);

			TextView category = (TextView) view.findViewById(categoryResourceId);
			TextView total = (TextView) view.findViewById(totalResourceId);

			category.setText(categories.get(position).getName());
			total.setText(Globals.toDecimalText(categories.get(position).getAmount()));

			return view;
		}
	}
}
